<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

	public function index()
	{
		$this->init();
	}

	function init($valid = false){
		$data['valid'] = $valid;
		$data['main_content'] = 'login/register.php';
		$this->load->view('includes/template.php',$data);
	}

	function create_user(){

		$userdata = array(
			'first_name' => $this->input->post('first_name'),
			'last_name' => $this->input->post('last_name'),
			'username' => $this->input->post('username'),
			'email' => $this->input->post('email'),
			'password' => md5($this->input->post('password'))
		);

		$this->load->model('register_model'); 
		$query = $this->register_model->validate_new_user($userdata);
		if ($query) {
			$this->init(true);
		}

	}


	function validate_new_user(){

		$this->load->library('form_validation');
		$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
		$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
		$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[15]|is_unique[users.username]');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[confirm_password]');
		$this->form_validation->set_rules('confirm_password', 'Confim Password', 'trim|required');
		$this->form_validation->set_error_delimiters('<p class="valid"><em>', '</em></p>');

		if ($this->form_validation->run() == FALSE){
			$this->form_validation->set_message('matches', 'Your Passwords Do Not Match');
			$this->init();
		}else{
			$this->create_user();
		}

		$this->form_validation->set_error_delimiters('<span class="validation">', '</span>');

	}

	

}
